#include "nr_client_device_tree.h"


SetTreewidget::SetTreewidget(nr_device_item_server &item_server, QWidget *parent)
	:QTreeWidget(parent)
    ,m_item_server(item_server)
    //,m_object_manager(nr_object_manager::instance())
{
    // nr_error_t status = this->object_manager().load();
     //Q_ASSERT(status==NR_ERROR_NONE);

    //SetTree();
    this->set_device_tree();
}

SetTreewidget::~SetTreewidget()
{

}


void SetTreewidget::SetTree()
{
	this->setEditTriggers(QAbstractItemView::NoEditTriggers);
	this->setColumnCount(1);
	//this->header()->setResizeMode(QHeaderView::ResizeToContents);
	
	// The tree header
	QStringList str_list;
	str_list << tr("Device List");
	this->setHeaderLabels(str_list);

	// Initialize the device tree, Insert the root device item
	str_list.clear();
	str_list <<  tr("Nport_5200");

	QTreeWidgetItem *item = new QTreeWidgetItem(str_list);
	this->insertTopLevelItem(0, item);

	str_list.clear();
	str_list << tr("ACR_220");
	QTreeWidgetItem *child_item = new QTreeWidgetItem(str_list);
	item->insertChild(0,child_item);

	str_list.clear();
	str_list << tr("ACR_330");
	child_item = new QTreeWidgetItem(str_list);
	item->insertChild(0,child_item);

	// Insert the root device item
	str_list.clear();
	str_list <<  tr("Nport_5201");

	item = new QTreeWidgetItem(str_list);
	this->insertTopLevelItem(0, item);
	
	// Insert the root device item
	str_list.clear();
	str_list <<  tr("Nport_5202");
	item = new QTreeWidgetItem(str_list);
	this->insertTopLevelItem(0, item);
	
	// Insert the child device item
	str_list.clear();
	str_list <<  tr("APMD-701");
	child_item = new QTreeWidgetItem(str_list);
	//QTreeWidgetItem* child_item = new QTreeWidgetItem(str_list);
	item->insertChild(0, child_item);
	//Insert the child device item 2
	str_list.clear();
	str_list << tr("APMD-702");
	child_item = new QTreeWidgetItem(str_list);
	item->insertChild(0,child_item);
	
	//Insert the child and the root into tree

	// Insert the root device item
	str_list.clear();
	str_list <<  tr("Nport_5203");

	item = new QTreeWidgetItem(str_list);
	this->insertTopLevelItem(0, item);
}

bool SetTreewidget::set_deviceitemserver_topnode()
{
    QStringList str_list;

    // Initialize the device tree, Insert the root device item
    nr_device_item_server &device_item_server = this->device_item_server();//this->object_manager().device_item_manager().root_item();
//     device_item_server.id();
//     device_item_server.name();
//     QString::number( device_item_server.device_type()->id() );//all 3 para in device_item_server
    str_list.clear();
    str_list <<  device_item_server.name();//only show the name to TreeView
    DeviceTreeWidgetItem *root_item = new DeviceTreeWidgetItem(str_list);
    root_item->object_device() = &device_item_server;
    root_item->treenode_type() = DeviceTreeWidgetItem::nr_device_item_server;
    this->insertTopLevelItem(0, root_item);

    foreach(nr_device_group* device_group, device_item_server.groups())
    {
        str_list.clear();
        str_list << device_group->name();//only 1 para
        DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
        child_item->object_device() = device_group;
        child_item->treenode_type() = DeviceTreeWidgetItem::nr_device_group;
        this->set_devicegroup_treenode(*child_item, *device_group);

        root_item->insertChild(0, child_item);
    }

    return true;
}

bool SetTreewidget::set_devicegroup_treenode(DeviceTreeWidgetItem &father_item, nr_device_group &device_group)
{
    QStringList str_list;
    foreach(nr_device_group* other_device_group, device_group.groups())
    {
        str_list.clear();
        str_list << other_device_group->name();//only 1 para
        DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
        child_item->object_device() = other_device_group;
        child_item->treenode_type() = DeviceTreeWidgetItem::nr_device_group;
        this->set_devicegroup_treenode(*child_item, *other_device_group);

        father_item.insertChild(0, child_item);
    }
    foreach(nr_device_item* device_item, device_group.items())
    {
        str_list.clear();
//         device_item->id();
//         device_item->name();
//         QString::number( device_item->device_type()->id() );//all 3 para in device_item
        str_list << device_item->name();
        DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
        child_item->object_device() = device_item;
        child_item->treenode_type() = DeviceTreeWidgetItem::nr_device_item;
        this->set_deviceitem_treenode(*child_item, *device_item);

        father_item.insertChild(0, child_item);
    }
    return true;
}

bool SetTreewidget::set_deviceitem_treenode(DeviceTreeWidgetItem &father_item, nr_device_item &device_item)
{
    QStringList str_list;
//     foreach(nr_device_property* device_property, device_item.properties())
//     {
//         str_list.clear();
//         QString::number( device_property->id() );
//         device_property->value().toString();//all 2 para in Property or PropertyGroup
//         str_list << device_property->value().toString();
//         DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
//         child_item->object_device() = device_property;
//         child_item->treenode_type() = DeviceTreeWidgetItem::treenode_type_t::nr_device_property;
//         if ( device_property->property_type_to_string() == "PropertyGroup" )
//         {
//             this->set_property_tree_node(*child_item, *device_property);
//         }
// 
//         father_item.insertChild(0, child_item);
//     }
    foreach(nr_device_item* other_device_item, device_item.items())
    {
        str_list.clear();
//         other_device_item->id();
//         other_device_item->name();
//         QString::number( other_device_item->device_type()->id() );//all 3 para in device_item
        str_list << other_device_item->name();
        DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
        child_item->object_device() = other_device_item;
        child_item->treenode_type() = DeviceTreeWidgetItem::nr_device_item;
        this->set_deviceitem_treenode(*child_item, *other_device_item);

        father_item.insertChild(0, child_item);
    }
    return true;
}

bool SetTreewidget::set_property_tree_node(DeviceTreeWidgetItem &father_item, nr_device_property &device_property)
{
    QStringList str_list;
    foreach(nr_device_property* other_device_property, device_property.children())
    {
        str_list.clear();
//         QString::number( other_device_property->id() );
//         other_device_property->value().toString();//all 2 para in Property or PropertyGroup
        str_list << other_device_property->value().toString();
        DeviceTreeWidgetItem *child_item = new DeviceTreeWidgetItem(str_list);
        child_item->object_device() = other_device_property;
        child_item->treenode_type() = DeviceTreeWidgetItem::nr_device_property;

        Q_ASSERT( other_device_property->property_type_to_string() == "Property" );

        father_item.insertChild(0, child_item);
    }

    return true;
}

bool SetTreewidget::set_device_tree()
{
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->setColumnCount(1);

    QStringList str_list;
    str_list << tr("Device List");
    this->setHeaderLabels(str_list);

    this->set_deviceitemserver_topnode();

    return true;
}
